package com.ziq.baselib.test.io

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.os.Environment
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.FileUtils
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import com.ziq.base.utils.LogUtil
import com.ziq.base.utils.StorageUtil
import com.ziq.baselib.databinding.ActivityIoStreamBinding
import dagger.hilt.android.AndroidEntryPoint
import java.io.*
import java.lang.StringBuilder


@AndroidEntryPoint
class IoStreamActivity : BaseActivity() {

    private val binding : ActivityIoStreamBinding by binding()

    @RequiresApi(Build.VERSION_CODES.N)
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root
        val txt = StorageUtil.getCacheFile(this, "/bb/text2.txt")
        val stringBuilder = StringBuilder()
        stringBuilder.append("ExternalStorageDirectory:${Environment.getExternalStorageDirectory()}")
        stringBuilder.append("\nExternalStorageDirectory:${Environment.getDataDirectory()}")
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            stringBuilder.append("\nExternalStorageDirectory r:${Environment.getStorageDirectory()}")
        }
        stringBuilder.append("\nExternalStorageDirectory:${Environment.getRootDirectory()}")
        stringBuilder.append("\nExternalStorageDirectory:${Environment.getDownloadCacheDirectory()}")
        stringBuilder.append("\ndataDir:${this.dataDir}")
        stringBuilder.append("\ncacheDir:${this.cacheDir}")
        stringBuilder.append("\ncodeCacheDir:${this.codeCacheDir}")
        stringBuilder.append("\nfilesDir:${this.filesDir}")
        stringBuilder.append("\nobbDir:${this.noBackupFilesDir}")
        stringBuilder.append("\nobbDir:${this.obbDir}")
        stringBuilder.append("\nobbDir:${this.externalCacheDir}")
        stringBuilder.append("\nobbDir:${this.externalCacheDirs}")
        stringBuilder.append("\nobbDir:${this.externalMediaDirs}")
        LogUtil.i("storage", stringBuilder.toString())
        binding.btnReadFile.click {
            val fis = FileInputStream(txt)
//            fis.apply {
//                var data:Int = 0
//                while ((this.read().also { data = it }) != -1){
//                    LogUtil.i("FileInputStream", "${data.toChar()}")
//                }
//                this.read()
//                this.close()
//            }

            val isr = InputStreamReader(fis, Charsets.UTF_8)
//            LogUtil.i("InputStreamReader", "${isr.readLines()}")
            BufferedReader(isr).apply {
                LogUtil.i("BufferedReader", "${this.readLine()}")
            }
        }
        binding.btnWriteFile.click {
            val fos = FileOutputStream(txt, true)
            val fow = OutputStreamWriter(fos, Charsets.UTF_8)
            BufferedWriter(fow).apply {
                this.append("ike")
                this.flush()
                this.close()
            }
        }
    }

    override fun onStart() {
        super.onStart()
    }
}